<?php
declare (strict_types=1);

namespace app\controller\home;

use app\common\controller\BaseController;
use app\model\home\Duty as dutyModel;
use think\App;
use \Throwable;

class Duty extends BaseController
{
    protected $model;

    public function __construct(App $app, dutyModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }


    /**
     * 列表查询
     * @return array
     */
    public function query()
    {
        $where = [];
        $page = input('page/d', 1);
        $pageSize = input('page_size/d', 20);
        $this->params['member_id'] = $this->memberId;
        $status = input('status/d', 1);
        $reType = input('re_type/d', 1);
        $where['re_type'] = $reType;
        $where['status'] = $status;
        $order = "duty_date DESC";
        $fields = 'id,re_id,re_type,store_id,store_id store_name,re_id user,duty_date,duty_status,position,start_timestr,end_timestr';
        return $this->model->getList($where, $fields, $order, $page, $pageSize, $this->params);
    }

    /**
     * 新建
     * POST
     * @return array
     */
    public function create()
    {
        try {
            validate(\app\validate\home\Duty::class)
                ->scene('create')
                ->check($this->params);
            $this->params['member_id'] = $this->memberId;
            return $this->model->add($this->params);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return array
     * 顶班
     */
    public function enable()
    {
        try {
            $this->params['member_id'] = $this->memberId;
            return $this->model->changeStatus($this->params, 2);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return array
     * 抛班
     */
    public function disable()
    {
        try {
            $this->params['member_id'] = $this->memberId;
            return $this->model->changeStatus($this->params, 3);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return mixed
     * 每周当值
     */
    public function week()
    {
        $where = [];
        $where[] = ['duty_status', 'in', [1, 2]];
        $where[] = ['status', '=', 1];
        $where[] = ['re_type', '=', 1];
        $page = input('page/d', 1);
        $pageSize = input('page_size/d', 10);
        $storeType = input('store_type/d', 1);
        $where[] = ['store_id', '=', $storeType];
        return $this->model->weekData($where, $page, $pageSize, $this->params);
    }
}
